package com.my.lucky.repository;

import com.my.lucky.entity.StockLimitUp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 股票涨停数据Repository接口
 */
@Repository
public interface StockLimitUpRepository extends JpaRepository<StockLimitUp, Long>, JpaSpecificationExecutor<StockLimitUp> {

    /**
     * 根据股票代码查找涨停记录
     */
    List<StockLimitUp> findByStockCode(String stockCode);


    @Query("SELECT s FROM StockLimitUp s WHERE s.stockCode IN (:stockCode)")
    List<StockLimitUp> findByStockCodeIn(@Param("stockCode") List<String> stockCodes);


    /**
     * 根据股票代码和记录时间范围查找涨停记录
     */
    List<StockLimitUp> findByStockCodeAndRecordTimeBetween(String stockCode, LocalDateTime startTime, LocalDateTime endTime);

    /**
     * 根据记录时间范围查找涨停记录
     */
    List<StockLimitUp> findByRecordTimeBetween(LocalDateTime startTime, LocalDateTime endTime);

    /**
     * 根据涨停类型查找记录
     */
    List<StockLimitUp> findByLimitUpType(String limitUpType);

    /**
     * 查找指定日期的涨停记录，按排名位置排序
     */
    @Query("SELECT s FROM StockLimitUp s WHERE DATE(s.recordTime) = DATE(:recordDate) ORDER BY s.sealOrderAmount DESC")
    List<StockLimitUp> findByRecordDateOrderBySealOrderAmountDesc(@Param("recordDate") LocalDateTime recordDate);

    /**
     * 查找连续涨停天数大于指定值的记录
     */
    @Query("SELECT s FROM StockLimitUp s WHERE s.consecutiveLimitUpCount LIKE %:days%")
    List<StockLimitUp> findByConsecutiveLimitUpDaysGreaterThan(@Param("days") String days);

    /**
     * 根据涨停原因关键词查找记录
     */
    @Query("SELECT s FROM StockLimitUp s WHERE s.limitUpReason LIKE %:keyword%")
    List<StockLimitUp> findByLimitUpReasonContaining(@Param("keyword") String keyword);

    /**
     * 查找今日涨停记录
     */
    @Query("SELECT s FROM StockLimitUp s WHERE DATE(s.recordTime) = CURRENT_DATE ORDER BY s.rankPosition ASC")
    List<StockLimitUp> findTodayLimitUpStocks();


    /**
     * 查找今日涨停记录
     */
    @Query("SELECT s FROM StockLimitUp s WHERE s.recordTime BETWEEN :startDate AND :endDate ORDER BY s.rankPosition ASC")
    List<StockLimitUp> findTodayLimitUpStocksAndRecordTimeBetween(@Param("startDate") LocalDateTime startDate,
                                                                  @Param("endDate") LocalDateTime endDate);

    /**
     * 统计指定日期的涨停股票数量
     */
    @Query("SELECT COUNT(s) FROM StockLimitUp s WHERE DATE(s.recordTime) = DATE(:recordDate)")
    Long countByRecordDate(@Param("recordDate") LocalDateTime recordDate);


    /**
     * 删除recordTime大于当天0点的涨停股票记录
     */
    @Modifying
    @Transactional
    @Query("DELETE FROM StockLimitUp s WHERE s.recordTime > CURRENT_DATE")
    void deleteByRecordTimeAfterToday();


    @Modifying
    @org.springframework.transaction.annotation.Transactional
    @Query(value = "UPDATE stock_limit_up sqr " +
            "JOIN stocks s ON sqr.stock_code = s.code " +
            "SET sqr.rank_position = s.rank_position,sqr.old_rank_position = s.old_rank_position,sqr.main_capital_flow = s.main_capital_flow,sqr.capital_flow = s.capital_flow ,sqr.main_cost = s.main_cost  ,sqr.all_concept = s.concept  " +
            "WHERE s.create_time > CURRENT_DATE AND sqr.create_time > CURRENT_DATE", nativeQuery = true)
    void updateByQueryTimeAfterTodayFromStocks();

} 